<?php 
@session_start();
require_once '../sub/init.php';
if(!is_mobile())header("Location: login.php");
require_once ZEAI.'cache/config_adm.php';
require_once ZEAI.'cache/config_wxgzh.php';
require_once ZEAI.'sub/conn.php';
$endip = getip();
$rt = $db->query("SELECT ipurl FROM ".__TBL_IP__." WHERE ipurl='$endip'");
if($db->num_rows($rt))json_exit(array('flag'=>0,'msg'=>'访问已被拒绝'));
$ifwxlogin=(!empty($_ZEAI['wx_gzh_appid']) && !empty($_ZEAI['wx_gzh_appsecret']) && !empty($_GZH['wx_gzh_token']))?true:false;
if($submitok=='ajax_weixinlogin' && $ifwxlogin){
	if(str_len($cook_adm_openid)<20)json_exit(array('flag'=>0,'msg'=>'openid获取失败<br>请检查微信公众号是否配置正确<br>如未配置公众号请改为帐号密码登录'));
	$row = $db->ROW(__TBL_ADMIN__,"id,roleid,kind,truename,agentid,agenttitle,path_s,username,password","openid<>'' AND openid='$cook_adm_openid'");
	if ($row){
			$db->query("UPDATE ".__TBL_ADMIN__." SET endtime=".ADDTIME.",endip='$endip',logincount=logincount+1 WHERE id=".$row[0]);
			$_SESSION["admuid"]   = $row[0];
			$roleid               = $row[1];
			$_SESSION["kind"]     = $row[2];
			$_SESSION["truename"] = $row[3];
			$_SESSION["agentid"]  = intval($row[4]);
			$_SESSION["agenttitle"]= $row[5];
			$_SESSION["path_s"]    = $row[6];
			$_SESSION["admuname"] = $row[7];
			$_SESSION["admpwd"]   = $row[8];
			if ( !ifint($roleid) )callmsg("角色载入错误","-1");
			$rtD=$db->query("SELECT authoritylist,crmkind,title,sq_sh_bfb FROM ".__TBL_ROLE__." WHERE id=".$roleid);
			if ($db->num_rows($rtD)){
				$rowD = $db->fetch_array($rtD,'num');
				$_SESSION["authoritylist"] = $rowD[0];
				$_SESSION["title"] = dataIO($rowD[2],'out');
				$_SESSION["sq_sh_bfb"] = $rowD[3];
			}else{
				json_exit(array('flag'=>0,'msg'=>'角色载入错误'));
			}
			if ($_SESSION["kind"] == 'crm'){
				$_SESSION["crmkind"]  = $rowD[1];
				if(ifint($_SESSION["agentid"])){
					$row = $db->ROW(__TBL_CRM_AGENT__,"flag,areaid","id=".$_SESSION["agentid"],'num');
					if ($row[0]!=1)json_exit(array('flag'=>0,'msg'=>'门店【'.$_SESSION["agenttitle"].'】已停止服务，请用超级管理员帐号进入开启'));	
					$_SESSION["agent_areaid"]=$row[1];
				}else{
					json_exit(array('flag'=>0,'msg'=>'门店载入错误'));
				}
			}
			$url= 'm_adm.php';
			AddLog2('(手机端)后台《微信一键登录》成功'.'【'.$_SESSION["admuname"].'（id:'.$_SESSION["admuid"].'）】IP：'.$endip);
			//setcookie("adm_loginnum",0,time()+720000,"/",$_ZEAI['CookDomain']);
			json_exit(array('flag'=>1,'msg'=>'已登录','url'=>$url));
	}else{
		json_exit(array('flag'=>0,'msg'=>'您当前微信还没有绑定后台帐号<br>请联系管理员进行相关帐号绑定'));
	}
}
if (is_weixin() && $ifwxlogin){
	if(str_len($cook_adm_openid)>20){
		$server_openid = $cook_adm_openid;
	}else{
		$server_openid = wx_get_openid(0);
		setcookie("cook_adm_openid",$server_openid,time()+31536000,"/",$_ZEAI['CookDomain']);
	}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $_ADM['admSiteName']; ?></title>
<?php echo HEADMETA; ?>
<meta name="generator" content="zeai.cn V8" />
<script src="<?php echo RES;?>www_zeai_cn.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="<?php echo RES;?>m/js/m.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<link href="<?php echo RES;?>m/css/m.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css" />
<link href="css/m_login.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css">
<style>
.loginbtn,.tablee td.tbg{background-color:<?php echo $_ZEAI['mob_mbcolor'];?>}
.tablee td.tbg{background-image:url("<?php echo RES;?>m/img/my_bg.png");background-size:100%}
<?php if (is_weixin() && $ifwxlogin){echo '.blank{height:60px}';}else{echo '.blank{height:-webkit-calc(50vh - 190px)';}?>
</style>
</head>
<body>
<div class="blank"></div>
<form name="WWWzeaiCN" method="post" action="login.php" onSubmit="return chkform()" autoComplete="off">
<table border="0" align="center" cellpadding="5" cellspacing="0" class="tablee">
<tr><td height="40" align="center" class="tbg"><div class="title"><?php echo $_ADM['admSiteName']; ?> <font><?php echo $_ZEAI['ver'];?></font></div></td></tr>
<tr><td height="5" align="left"></td></tr>
<tr>
<td height="50" align="left"><i class="ico">&#xe645;</i><input name="uname" id="uname" class="login "  maxlength="20" autoComplete="off" placeholder="请输入登录帐号" /></td>
</tr>
<tr>
<td height="50" align="left"><i class="ico">&#xe620;</i><input name="pwd" id="pwd"  type="password" class="login " maxlength="20" autoComplete="off" placeholder="请输入登录密码"  />
</td>
</tr>
<tr>
<td height="55" align="left"><table border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left">
<i class="ico verify">&#xe6c3;</i><input name="verifycode"  id="verifycode" class="login "  maxlength="4" autocomplete="off" placeholder="验证码"  />
</td>
<td width="70" align="right" style="padding-left:10px" title="看不清楚请点击刷新验证码"><img src="../sub/authcode.php" alt="看不清楚请点击刷新验证码" name="gylverify" align="middle" id="gylverify" style="cursor : pointer;" onclick="ReloadCode()" /></td>
<td width="38" align="right" style="padding-left:5px;cursor:pointer;" title="看不清楚请点击刷新验证码" onclick="ReloadCode()"><img src="images/reload.png" width="28" height="32" onclick="ReloadCode()" /></td>
</tr>
</table></td>
</tr>
<tr>
<td align="center" valign="top" class="btntd"><input type="submit" value="登 录" class="loginbtn" /></td>
</tr>
<?php if (is_weixin() && $ifwxlogin){?>
<tr>
<td align="center" valign="top" class="otherlogin">
<em class="weixin" onClick="otherlogin('weixin');"><span class="ico">&#xe607;</span><font>微信一键登录</font></em>
</td>
</tr>
<?php }?>
</table>
<input type="hidden" id="loginkind" value="m" />
</form>
</body>
</html>
<script src="js/login.js?<?php echo $_ZEAI['cache_str'];?>" ></script>
<?php
function AddLog2($c) {
	global $db,$_SESSION;
	$session_uname   = $_SESSION["admuname"];
	$session_kind    = $_SESSION["kind"];//adm,crm
	$session_agentid = intval($_SESSION["agentid"]);
	$session_agenttitle  = $_SESSION["agenttitle"];
	$kind=($session_kind=='crm')?2:1;$c=dataIO($c,'in',2000);
	$db->query("INSERT INTO ".__TBL_LOG__."  (username,kind,content,addtime,agentid,agenttitle) VALUES ('$session_uname',$kind,'$c',".ADDTIME.",$session_agentid,'$session_agenttitle')");
}
ob_end_flush();?>